import com.javasm.bike.bean.*;
import consts.BikeConstants;
import dao.BikeDao;
import impl.BikeDaoImol;

import java.util.Scanner;

/**
 * @classname:SharedBikeMgr
 * @description:
 * @author: Mr.Z
 * @date: 2020/11/30 21:06
 * @version: 0.1
 * @since:1.8
 */
public class SharedBikeMgr {
   private SharedBikeCompany[] sharedBikeCompanies;
   private Scanner input;
   private BikeDao bikeDao;

    public void init() {
        bikeDao = new BikeDaoImol();
        input  = new Scanner(System.in);
        SharedBike[] sharedBikes1 = new OfoBike[5];
        SharedBike[] sharedBikes2 = new MobaiBike[5];
        SharedBike[] sharedBikes3 = new HaloBike[5];
        for (int i = 1; i <= 5; i++) {
            sharedBikes1[i - 1] = new OfoBike(i, BikeConstants.OFO_BIKE_NAME + i, 1, null);
            sharedBikes2[i - 1] = new MobaiBike(i, BikeConstants.MOBAI_BIKE_NAME + i, 1, null);
            sharedBikes3[i - 1] = new HaloBike(i, BikeConstants.HALO_BIKE_NAME + i, 1, null);
        }
        sharedBikeCompanies = new SharedBikeCompany[3];
        sharedBikeCompanies[0] = new SharedBikeCompany(1, BikeConstants.OFO_COMPANY_NAME, sharedBikes1, 5, 1000);
        sharedBikeCompanies[1] = new SharedBikeCompany(2, BikeConstants.MOBAI_COMPANY_NAME, sharedBikes2, 5, 2000);
        sharedBikeCompanies[2] = new SharedBikeCompany(3, BikeConstants.HALO_COMPANY_NAME, sharedBikes3, 5, 10000);
    }

    public void startMenu() {
        System.out.println("              欢迎使用迷你共享单车");
        System.out.println("*********************************************");
        System.out.println("              1.投放Bike");
        System.out.println("              2.查看Bike");
        System.out.println("              3.删除Bike");
        System.out.println("              4.借出Bike");
        System.out.println("              5.归还Bike");
        System.out.println("              6.Bike排行榜");
        System.out.println("              7.退出");
        System.out.println("*********************************************");
        System.out.println("请选择:");
        int choice = input.nextInt();
        switch (choice) {
            case 1:
                addModule();
                break;
            case 2:
                bikeDao.selectBike(sharedBikeCompanies);
                break;
            case 3:
               deleteModule();
                break;
            case 4:
                borrowModule();
                break;
            case 5:
                retrunModule();
                break;
            case 6:
                bikeDao.rankBike(sharedBikeCompanies);
                break;
            case 7:
                break;
        }
        returnMenu();
    }

    private void retrunModule() {
        int i = chooseCompany();
        bikeDao.returnBike(sharedBikeCompanies[i-1],input);
    }

    private void borrowModule() {
        int i = chooseCompany();
        bikeDao.borrowBike(sharedBikeCompanies[i-1],input);
    }

    private void deleteModule() {
        int i = chooseCompany();//i的值充当公司的索引
        bikeDao.deleteBike(sharedBikeCompanies[i-1],input);
    }


    private void addModule() {
        int i = chooseCompany();//i的值充当公司的索引
        System.out.println("请输入要投放单车数量");
        int addNum = input.nextInt();
        bikeDao.addBike(addNum, sharedBikeCompanies[i-1]);

    }

    private int  chooseCompany() {
        System.out.println("1."+BikeConstants.OFO_COMPANY_NAME);
        System.out.println("2."+BikeConstants.MOBAI_COMPANY_NAME);
        System.out.println("3."+BikeConstants.HALO_COMPANY_NAME);
        System.out.println("请选择要操作的公司");
        return input.nextInt();
    }

    public  void returnMenu(){
        System.out.println("输入0返回");
        int num = input.nextInt();
        startMenu();

    }
    public SharedBikeMgr() {
        init();
    }
}
